package com.cib.process.chat.core.selector;

import com.cib.process.bean.Actions;
import com.cib.process.bean.ChannelType;
import com.cib.process.bean.IvrRequestServer;
import com.cib.process.bean.QueryRequestParam;
import com.cib.process.chat.core.AbstractActionSeletor;
import com.cib.process.util.RedisKey;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import sun.security.krb5.internal.PAData;

import java.net.Proxy;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 密码处理稍复杂
 */
public class PasswordActionSeletor extends AbstractActionSeletor {
    private boolean collectConversation =false;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //@Value("${redis.pswd.timeout:15}");
    private Long time;
    //@Value("${wbg.private.key}")
    private String key;


    @Override
    public void paramSelector(IvrRequestServer ivrRequestServer, QueryRequestParam queryRequestParam) {
        String lastAction = ivrRequestServer.getLastAction();
        if (Actions.PWD.getCode().endsWith(lastAction)){
            String token = ivrRequestServer.getToken();
            ChannelType type = ChannelType.instanceOf(token);
            Map<String, String> lastResult = ivrRequestServer.getLastResult();
            String password = lastResult.get("password");
            switch (type){
                case WBG:
                    break;
                case IVR:   //将密码存入redis
                    if (!"#".equals(password)&& StringUtils.isNotBlank(password)){
                        redisTemplate.opsForList().leftPush(RedisKey.getPwdKey(ivrRequestServer.getSessionId()),password);
                        redisTemplate.expire(RedisKey.getPwdKey(ivrRequestServer.getSessionId()),time, TimeUnit.MINUTES);
                    }

                    break;
            }
            handleLastResult(ivrRequestServer,queryRequestParam);
        }


    }



    @Override
    public void setCollectConversation(QueryRequestParam queryRequestParam) {
        queryRequestParam.setCollectConversation(collectConversation);
    }

    @Override
    protected String setQueryText(Map<String, String> lastResult) {
        return "#".equals(lastResult.get("password"))?"#":"@";
    }
}
